package net.xdclass.order.controller;


import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.common.JsonData;
import net.xdclass.common.constant.Cons;
import net.xdclass.common.enums.BizCodeEnum;
import net.xdclass.common.enums.ProductOrderStateEnum;
import net.xdclass.order.config.AliPayConfig;
import net.xdclass.order.enums.ClientType;
import net.xdclass.order.enums.ProductOrderPayTypeEnum;
import net.xdclass.order.model.request.ComfirmOrderRequest;
import net.xdclass.order.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  订单
 * </p>
 *
 * @author yaya
 * @since 2023-12-31
 */
@Api(tags = "订单模块")
@Slf4j
@RestController
@RequestMapping("/api/order/v1")
public class ProductOrderController {

    @Autowired
    private ProductOrderService orderService;

    @GetMapping("/page")
    public JsonData page(@ApiParam("当前页") @RequestParam(value = "page", defaultValue = "1", required = false) int page,
                         @ApiParam("页大小") @RequestParam(value = "size", defaultValue = "10", required = false) int size,
                         @ApiParam(value = "订单状态") @RequestParam(value = "state", required = false) ProductOrderStateEnum state){
        return orderService.page(page, size, state);
    }

    /**
     * no login
     * @param outTradeNo
     * @returnq
     */
    @ApiOperation("RPC-查询订单状态")
    @GetMapping("/query/state")
    public JsonData queryOrderState(@RequestParam("outTradeNo") String outTradeNo){
        ProductOrderStateEnum state = orderService.selectOrderState(outTradeNo);
        if (state == null){
            return JsonData.buildResult(BizCodeEnum.ORDER_CONFIRM_NOT_EXIST);
        }
        return JsonData.buildSuccess(state);
    }

    @ApiOperation("提交订单")
    @PostMapping("/comfirm")
    public void comfirmOrder(@ApiParam("订单对象") @RequestBody ComfirmOrderRequest order,
                             HttpServletResponse response){
        JsonData jsonData = orderService.comfirmOrder(order);
        if (Cons.SUCCESS.equals(jsonData.getCode())){
            ProductOrderPayTypeEnum payType = order.getPayType();
            ClientType clientType = order.getClientType();
            // 支付宝支付
            if (ProductOrderPayTypeEnum.ALIPAY.equals(payType)) {
                log.info("支付宝订单创建成功。【{}】", order);
                writeData(response, jsonData);
            }else if (ProductOrderPayTypeEnum.WECHAT.equals(payType)){
                log.info("微信订单创建成功。【{}】", order);
            }
        }else {
            log.warn("订单创建失败 【{}】", order);
        }
    }

    /**
     * 支付宝支付是跳转页面
     * @param response
     * @param jsonData
     */
    private void writeData(HttpServletResponse response, JsonData jsonData) {
        try (PrintWriter writer = response.getWriter()){
            response.setContentType("text/html;charset=utf8");
            writer.write(jsonData.getData().toString());
            writer.flush();
        } catch (IOException e) {
            log.warn("数据写出失败。【{}】", e);
        }
    }

}

